import courseApi from "@/views/api/course/course";
import userManageApi from '@/views/api/system/user/userManage'
import { mapGetters } from "vuex";

export default {
    data() {
        return {
            courseList: [],
            teacherList: [],
            list: [],
            listLoading: false,
            isAdd: false,
            dialogFormVisible: false,
            currentRow: '',
            total: 0,
            listQuery: {
                page: 1,
                pageSize: 5,
                courseName: undefined,
                teacherId: undefined,
                studentId: undefined
            },
        }
    },
    created() {
        this.init()
    },
    computed: {
        ...mapGetters(['userInfo'])
    },
    methods: {
        init(){
            this.fetchData()
        },
        async fetchData() {
            this.listLoading = true
            const { data } = await userManageApi.getTeacherList()
            this.teacherList = data
            courseApi.getCourseList(this.listQuery).then(response => {
                this.courseList = response.data.records
                this.list = this.courseList.filter(item => {
                    if (this.userInfo.courseIds === null || !this.userInfo.courseIds.includes(item.id)) {
                        item.teacherName = this.teacherList.find(value => value.id === item.teacherId).username
                        return item
                    }
                })
                this.listLoading = false
                this.total = response.data.total
            })
        },
        select(row) {
            this.listQuery.studentId = this.$store.getters.userInfo.id
            userManageApi.addCourse(row.id, this.listQuery.studentId).then(response => {
                this.$store.commit('user/SET_USER_INFO', response.data)
            })
            courseApi.selectCourse(row.id, this.listQuery.studentId).then(() => {
                this.$message({
                    message: '选课成功,可在个人信息中查看选课信息',
                    type: 'success'
                })
                this.fetchData()
            })
        },
        add() {
            this.isAdd = true
            this.resetForm()
            this.dialogFormVisible = true
        },
        search() {
            this.fetchData()
        },
        reset() {
            this.listQuery.courseName = ''
            this.listQuery.teacherId = ''
            this.fetchData()
        },
        changeSize(limit) {
            this.listQuery.pageSize = limit
            this.fetchData()
        },
        fetchPage(page) {
            this.listQuery.page = page
            this.fetchData()
        },
        fetchNext() {
            this.listQuery.page = this.listQuery.page + 1
            this.fetchData()
        },
        fetchPrev() {
            this.listQuery.page = this.listQuery.page - 1
            this.fetchData()
        },
        handleCurrentChange(currentRow, oldCurrentRow) {
            this.currentRow = currentRow;
        }
    }
}
